/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Venda.java
 *
 * Created on 28/11/2011, 16:24:06
 */
package Telas;

import Pojo.TbCliente;
import Pojo.TbProduto;
import Pojo.TbVenda;
import Pojo.TbVendaProduto;
import Pojo.TbVendedor;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author jeferson
 */
public class Venda extends javax.swing.JDialog {
 
    private Date now = new Date();
    private DateFormat df = DateFormat.getDateInstance();
    
    /** Creates new form Venda */
    public Venda(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        
      String s = df.format(now);
      txtData.setText(s);
      
      System.out.println("Hoje é " + s);    
        
        
   
        
        
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        AplicacaoMusicalPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("AplicacaoMusicalPU").createEntityManager();
        tbClienteQuery = java.beans.Beans.isDesignTime() ? null : AplicacaoMusicalPUEntityManager.createQuery("SELECT t FROM TbCliente t");
        tbClienteList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tbClienteQuery.getResultList();
        tbProdutoQuery = java.beans.Beans.isDesignTime() ? null : AplicacaoMusicalPUEntityManager.createQuery("SELECT t FROM TbProduto t");
        tbProdutoList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tbProdutoQuery.getResultList();
        tbArtistaQuery = java.beans.Beans.isDesignTime() ? null : AplicacaoMusicalPUEntityManager.createQuery("SELECT t FROM TbArtista t");
        tbArtistaList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tbArtistaQuery.getResultList();
        tbProdutoQuery1 = java.beans.Beans.isDesignTime() ? null : AplicacaoMusicalPUEntityManager.createQuery("SELECT t FROM TbProduto t");
        tbProdutoList1 = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tbProdutoQuery1.getResultList();
        tbVendedorQuery = java.beans.Beans.isDesignTime() ? null : AplicacaoMusicalPUEntityManager.createQuery("SELECT t FROM TbVendedor t");
        tbVendedorList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : tbVendedorQuery.getResultList();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        labelVendedor = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        comboCliente = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        txtData = new javax.swing.JLabel();
        comboProduto = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        proQuantidade = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        proValor = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        venProdutosLista = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        comboArtista = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        comboVendedor = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Gravar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancelar");

        jLabel1.setText("Vendedor");

        labelVendedor.setText("jLabel2");

        jLabel2.setText("Cliente");

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tbClienteList, comboCliente);
        bindingGroup.addBinding(jComboBoxBinding);

        jLabel3.setText("Data");

        txtData.setText("jLabel4");

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${selectedItem.tbProdutoList}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, comboArtista, eLProperty, comboProduto);
        bindingGroup.addBinding(jComboBoxBinding);

        jLabel4.setText("Produto");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, comboProduto, org.jdesktop.beansbinding.ELProperty.create("${selectedItem.proQuantidade}"), proQuantidade, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jLabel5.setText("Quantidade");

        jLabel6.setText("Valor");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ, comboProduto, org.jdesktop.beansbinding.ELProperty.create("${selectedItem.proValor}"), proValor, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        jButton3.setText("Adicionar");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        venProdutosLista.setModel(produtos = new VendaTableModel(ListaProduto));

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, venProdutosLista, org.jdesktop.beansbinding.ObjectProperty.create(), venProdutosLista, org.jdesktop.beansbinding.BeanProperty.create("elements"));
        bindingGroup.addBinding(binding);
        binding.bind();
        jScrollPane1.setViewportView(venProdutosLista);

        jLabel7.setText("Total");

        lbTotal.setText("0");

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tbArtistaList, comboArtista);
        bindingGroup.addBinding(jComboBoxBinding);

        comboArtista.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboArtistaItemStateChanged(evt);
            }
        });

        jLabel8.setText("Artista");

        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, tbVendedorList, comboVendedor);
        bindingGroup.addBinding(jComboBoxBinding);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLabel8)
                                    .addGap(18, 18, 18)
                                    .addComponent(comboArtista, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
                                    .addComponent(jLabel4))
                                .addGroup(layout.createSequentialGroup()
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2)
                                        .addComponent(jLabel1))
                                    .addGap(18, 18, 18)
                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(comboCliente, 0, 168, Short.MAX_VALUE)
                                            .addGap(14, 14, 14))
                                        .addGroup(layout.createSequentialGroup()
                                            .addComponent(labelVendedor, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                            .addComponent(comboVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(393, 393, 393)
                                    .addComponent(jLabel3)
                                    .addGap(44, 44, 44)
                                    .addComponent(txtData))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addGap(12, 12, 12)
                                    .addComponent(comboProduto, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(37, 37, 37)
                                    .addComponent(jLabel5)
                                    .addGap(18, 18, 18)
                                    .addComponent(proQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jLabel6)
                                    .addGap(18, 18, 18)
                                    .addComponent(proValor, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jButton3)))
                            .addGap(151, 151, 151))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 802, Short.MAX_VALUE)
                            .addGap(151, 151, 151)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel7)
                            .addComponent(jButton2))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(lbTotal, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(162, 162, 162))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(labelVendedor)
                    .addComponent(comboVendedor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(txtData)
                    .addComponent(comboCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(comboArtista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(comboProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(proQuantidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(jButton3)
                    .addComponent(proValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbTotal)
                    .addComponent(jLabel7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addGap(114, 114, 114))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
//    TbVenda venda = new TbVenda();
//    venda.setVendCodcliente((TbCliente)comboCliente.getSelectedItem());
//    venda.setVendCodvendedor((TbVendedor)comboVendedor.getSelectedItem());
//    venda.setVendData(df.format(now));
//   
//    for (TbProduto tbProduto : ListaProduto) {
//        TbVendaProduto prodVenda= new TbVendaProduto();
//        prodVenda.setTbProduto(tbProduto);
//        prodVenda.setVendaQtde(tbProduto.getProQuantidade());
//        prodVenda.set
//    }
// 
//    
//   List<TbVendaProduto> listadeProdutos = new ArrayList<TbVendaProduto>();
//   
//   
//    
//    
//    
//    
//    
//    
//    
//    
//    venda.setTbVendaProdutoList();
//    



}//GEN-LAST:event_jButton1ActionPerformed

private void comboArtistaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboArtistaItemStateChanged
    
    
}//GEN-LAST:event_comboArtistaItemStateChanged

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
// TODO add your handling code here:
    produto=(TbProduto)comboProduto.getSelectedItem();
    produto.setProQuantidade(Integer.parseInt(proQuantidade.getText()));
    produto.setProValor(BigDecimal.valueOf(Double.valueOf(proValor.getText())));
    
    
    
    int flag =0;
    for (TbProduto tbProduto : ListaProduto) {
        if(tbProduto.getProCodigo()==produto.getProCodigo()){
            tbProduto=produto;
            flag=1;
        }
    }
    
    if(flag==0){
        ListaProduto.add(produto);
    }
    
    

    System.out.println("pro:"+produto.getProTitulo());
    System.out.println("valor: "+produto.getProQuantidade());
    System.out.println("qnt: "+produto.getProValor());
    
    
//    for (TbProduto tbProduto : ListaProduto) {
//        System.out.println(":"+tbProduto.getProTitulo());
//    }
//    
//    
    produtos= new VendaTableModel(ListaProduto);
    venProdutosLista.setModel(produtos);
    
    
    float total = 0;
    for (TbProduto tbProduto : ListaProduto) {
        Float f;
        f=tbProduto.getProValor().floatValue();
        total +=f*tbProduto.getProQuantidade();
    }
    lbTotal.setText(String.valueOf(total));
    
    
//    
//    
//    
//    
}//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Venda.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Venda dialog = new Venda(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    private TbProduto produto = new  TbProduto();
    private VendaTableModel produtos;
    
    private  List<TbProduto> ListaProduto = new ArrayList<TbProduto>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager AplicacaoMusicalPUEntityManager;
    private javax.swing.JComboBox comboArtista;
    private javax.swing.JComboBox comboCliente;
    private javax.swing.JComboBox comboProduto;
    private javax.swing.JComboBox comboVendedor;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelVendedor;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JTextField proQuantidade;
    private javax.swing.JTextField proValor;
    private java.util.List<Pojo.TbArtista> tbArtistaList;
    private javax.persistence.Query tbArtistaQuery;
    private java.util.List<Pojo.TbCliente> tbClienteList;
    private javax.persistence.Query tbClienteQuery;
    private java.util.List<Pojo.TbProduto> tbProdutoList;
    private java.util.List<Pojo.TbProduto> tbProdutoList1;
    private javax.persistence.Query tbProdutoQuery;
    private javax.persistence.Query tbProdutoQuery1;
    private java.util.List<Pojo.TbVendedor> tbVendedorList;
    private javax.persistence.Query tbVendedorQuery;
    private javax.swing.JLabel txtData;
    private javax.swing.JTable venProdutosLista;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
